﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Meraz.Core.Managers;
using Meraz.Web.Models;

using Ninject;

namespace Meraz.Web.Controllers
{
    public class FileController : Controller
    {
        [Inject]
        public FileManager FileManager { get; set; }

        //
        // GET: /Upload/

        public ActionResult Index()
        {
            FileModel model = new FileModel();
            model.Files = GetFiles();
            model.Message = "";
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FileModel model)
        {
            StudyKeyType studyKey = model.StudyKeyType;

            foreach (string file in Request.Files)
            {
                HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                if (hpf.ContentLength == 0)
                    continue;
                FileManager.Save(hpf.FileName, hpf.InputStream, studyKey);
            }
            switch (studyKey)
            {
                case StudyKeyType.Twitter:
                    return RedirectToAction("Sources", "Twitter");
            }

            model.Message = "File uploaded!";
            model.Files = GetFiles();
            return View("Index", model);
        }

        public List<FileInfo> GetFiles()
        {
            List<FileInfo> list = new List<FileInfo>();
            list.AddRange(FileManager.GetFiles(StudyKeyType.Twitter));
            return list;
        }
    }
}
